<?php
//NOT AJAX
function noAdmin()
{
	return '404 Not Found.';
}
function itemView()
{
	
	//DATABASE/VARS
	$categoryviewed = (get('cat') == 'null' ? 'all' : get('cat'));
	if(get('cat') != 'del')
	{
		$itemList = recoverItemList($categoryviewed);
	}
	else
	{
		$itemList = recoverDeletedItems();
	}
	$item = changeToOptions($itemList['itemID'], $itemList['itemID'], '');
	$categLists = recoverCategories();
	$categ = changeToOptions($categLists['catID'], $categLists['catName'], '');
	
	$display = '
	<div id="itemMain" class="grid_12 mainBox bordered">
		<div class="grid_4 alpha">
		<h1>Item View</h1>
			<div id="itemListContainer">
				<div id="itemList" rel="'.$categoryviewed.'">
					<select id="item" name="item" size="6" class="globalSelector">
						'.$item.'
					</select>
					<div class="catDiv leftAlign"><a href="'.INDEX.'control'.EXT.'sector=newItem" class="catLink" rel="popup">New Item Account</a>&nbsp;&nbsp;<a href="#" id="catShow" class="catLink">Categories</a></div>
				</div>
				<div id="catList">
					<select id="categ" name="category" size="6" class="globalSelector">
						<option value="all">*All Items</option>
						'.$categ.'
					</select>
					<div class="catDiv leftAlign"><a href="#" id="catLoad" class="catLink" rel="'.INDEX.'items'.EXT.'view=items">Load Category</a>&nbsp;&nbsp;<a href="'.INDEX.'items'.EXT.'view=items&cat=del" class="catLink">Deleted Items</a>&nbsp;&nbsp;<a href="#" class="catLink" id="catHide">Back</a></div>
				</div>
			</div>
		</div>
		<div class="padtop" class="grid_8 omega">
			<h3>Help:</h3>
			<p>
				To view and/or modify item accounts, click on your selection within the list of all items on the left side. To narrow down the item list per category, click the \'Categories\' link below the selection list. This will reveal an option for you to specify a category. Click \'Load Category\' to narrow down your search. You can pick your items once the page reloads. To modify/delete an item, click the \'Modify This Item\' button below and you will be linked to a page where you can modify item accounts. If you experience any technical difficulties, do not hesitate to send us an inquiry through our website.
			</p>
			<p class="rightAlign">
				- '.logo().'
			</p>
		</div>
	</div>';
	
	
	if(get('id') != 'null')
	//once DB Link is done -- pls add this condition: if db search != no results found or failure of query.
	{
		$itemInfo = recoverItemInfo(get('id'));
		
		if($itemInfo['queryStatus'] == 'DB Query success!')
		{
	// DATABASE/VARS <- item-specific
			$itemName = $itemInfo['itemID'][0];
			$itemDesc = $itemInfo['itemDesc'][0];
			$itemNotes = $itemInfo['notes'][0];
			$itemCat = $itemInfo['catName'][0];
			$itemActive = ($itemInfo['active'][0] == '0' ? 'Yes' : 'No');
			$itemCount = getItemCount($itemInfo['itemID'][0]);
			$itemCreated = sqlToReadable($itemInfo['created'][0]);
			$itemID = $itemInfo['itemID'][0];
	//----
			$display .= '
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox bordered">
				<div class="grid_12 alpha">
					<h3>'.$itemName.' Description</h3>
					<p>'.$itemDesc.'</p>
				</div>
				<div class="clear">
				<!--spanner-->
				</div>
				<div class="grid_6 alpha">
					<div class="grid_6 alpha">
					<h3>Status:</h3>
					</div>
					<div class="clear">
					<!--spanner-->
					</div>
					<div class="grid_2 alpha bold uppercase">
					Item Code:<br />Category:<br />Active?:<br />Created:<br />Inventory Count:
					</div>
					<div class="grid_4 omega">
					'.$itemID.'<br />'.$itemCat.'<br />'.$itemActive.'<br />'.$itemCreated.'<br />'.$itemCount.'
					</div>
				</div>
				<div class="grid_6 omega">
					<h3>Notes:</h3>
					<p>'.$itemNotes.'</p>
				</div>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox">
				<div class="centerAlign">
					<input type="button" value="Modify This Item" rel="'.INDEX.'control'.EXT.'sector=newItem&id='.$itemID.'" class="buttons" id="editItem"/>
				</div>
			</div>
			';	
		}
	}
	return $display;
}
function categoryView()
{
	$categLists = recoverCategories();
	
	$categoryviewed = (get('cat') == 'null' ? 'all' : get('cat'));
	$itemLists = recoverItemList($categoryviewed);
	$categ = changeToOptions($categLists['catID'], $categLists['catName'], get('cat'));
	
	//DATABASE -- category specific
	if(get('cat') == 'null' || get('cat') == 'new')
	{
		$numItems = '';
		$categoryInfo = '';
		$dateCreated = '';
		$itemsTotal = '';
		$itemList = '';		
	}
	else
	{
		$numItems = countItems($categoryviewed);
		$categoryInfo = recoverCategoryInfo($categoryviewed);
		$dateCreated = sqlToReadable($categoryInfo['dateCreated'][0]);
		$itemsTotal = recoverItemList($categoryviewed);
		$itemList = iterateTwoColumnedTable('itemID', $itemLists);
	}
	
	
	//---
	$display = '<div id="itemMain" class="grid_12 mainBox bordered" rel="'.INDEX.'items'.EXT.'view=categories">
		<div class="grid_4 alpha">
		<h1>Category View</h1>
			<div id="itemListContainer">
				<div id="catListed">
					<select id="categList" name="category" size="6" class="globalSelector">
						<option value="new">--New Category--</option>
						'.$categ.'
					</select>
				</div>
			</div>
		</div>
		<div class="grid_8 omega padtop">
			<h3>Help:</h3>
			<p>
				To add/modify category accounts, simply click on the category of your choice or \'New Category\' on the left. The page will reload with options for modification and addition of one or more categories. Note that you should supply at least one item account for a category to show itself on the item view (Catalogue &gt; Items). If you experience any technical difficulties, do not hesitate to send us an inquiry through our website.
			</p>
			<p class="rightAlign">
				- '.logo().'
			</p>
		</div>
	</div>';
	
	if(get('cat') != 'null')
	{//once DB Link is done -- pls add this condition: if db search != no results found or failure of query.
		if(get('cat') == 'new')
		{
			$display .= '
			<div class="clear">
			<!--spanner-->
			</div>
			<form id="newItemAndCategory" rel="'.INDEX.'control'.EXT.'sector=newItem">
			<div class="grid_12 mainBox bordered centerAlign bold uppercase catNewName">
				Create Category: <input type="text" name="category_New" id="newCategoryName" class="mainInput"/>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox borderbottom">
				<h3>Create New Item In this Category</h3>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			
			<div class="grid_12 mainBox bordered">
				<div class="grid_1 alpha">
				&nbsp;
				</div>
				<div class="grid_5">
					<div class="grid_2 alpha bold uppercase">
						Item Code:<br />Item Description:
					</div>
					<div class="grid_3 omega">
						<input type="text" id="newItemText" name="itemID"/><br />
						<textarea id="newDescArea" class="areas" name="description"></textarea>
					</div>
				</div>
				<div class="grid_5">
					<div class="grid_2 alpha bold uppercase">
						Notes:
					</div>
					<div class="grid_3 omega">
						<textarea name="notes" id="newNotesArea" class="areas"></textarea>
					</div>
				</div>
				<div id="extraspace" class="grid_1">
				&nbsp;
				</div>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox centerAlign">
			<input type="submit" class="buttons" value="Save" />
			</div>
			</form>
			';
		}
		else
		{
			$display .= '
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox bordered centerAlign bold uppercase catNewName">
				<form id="idRename" rel="'.INDEX.'items'.EXT.'view=categories&cat='.get('cat').'">Rename this category to: <input type="text" id="renameTo" name="catName" class="mainInput"/>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox">
				<div class="grid_1 alpha">
				&nbsp;
				</div>
				<div class="grid_5 borderbottom">
				<h3>Statistics</h3>
				</div>
				<div class="grid_5 borderbottom">
				<h3>Item List</h3>
				</div>
				<div class="grid_1 omega">
				&nbsp;
				</div>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			
			<div class="grid_12 mainBox bordered">
				<div class="grid_1 alpha">
				&nbsp;
				</div>
				<div class="grid_5">
					<div class="grid_2 alpha bold">
						Number of Items:<br />Created Last:
					</div>
					<div class="grid_3 omega">
						'.$numItems.'<br />'.$dateCreated.'
					</div>
				</div>
				<div class="grid_5">
					<table id="itemList">'.$itemList.'</table>
				</div>
				<div class="grid_1">
				&nbsp;
				</div>
			</div>
			<div class="clear">
			<!--spanner-->
			</div>
			<div class="grid_12 mainBox centerAlign">
			<input type="hidden" value="'.get('cat').'" name="rename" />
			<input type="submit" class="buttons" value="Save" /></form>
			<form id="idDelete" rel="'.INDEX.'items'.EXT.'view=categories"><input type="hidden" value="'.get('cat').'" name="delete" /><input type="submit" class="buttons" value="Delete" /></form>
			</div>
			';
		}
	}
	return $display;
}

function locationView()
{
	//LOCATION PAGE SETUP
	$tables = array();
	$tables[0] = 'inv_location';
	
	$whereClause = 'locName="UNSORTED"';
	
	$data = SQLCount($tables, $whereClause);
	
	if($data['COUNT(*)'][0] <= 0)
	{
		$tableName = 'inv_location';
		
		$fields = array();
		$fields[0] = 'locName';
		$fields[1] = 'locDesc';
		
		$values = array();
		$values[0] = 'UNSORTED';
		$values[1] = '*Not assigned';
		
		$data = SQLInsert($tableName, $fields, $values);
	}
	
/* 	--- check first if UNSORTED location is existing. If not, create  ----*/

	$locationList = recoverLocationList();
	
	$optionVals = ($locationList['queryStatus'] == 'DB Query success!' ? changeToOptions($locationList['locName'], $locationList['locDesc'], get('loc')) : '');
	
	$locContents = (get('location') == 'null' ? '
	<div class="grid_12 mainBox bordered centerAlign bold uppercase catNewName">
		<input type="hidden" name="act" value="new"/>
		Create New Location: <input type="text" id="renameTo" name="createNew" class="mainInput"/>
	</div>
	<div class="grid_12 mainBox bordered">
		Creating a new location will enable you to sort your actions whether an object is coming in or out of storage. Assigning and creating locations is key to the system\'s functions. Fill in the box above and simply click on the \'Save\' button below to continue.
	</div>' : '
	<div class="grid_12 mainBox bordered centerAlign bold uppercase catNewName">
		<input type="hidden" name="act" value="'.get('location').'"/>
		Rename This Location: <input type="text" id="renameTo" name="renameThis" class="mainInput"/>
	</div>
	<div class="grid_12 mainBox bold">
		Statistics:
	</div>
	<div class="clear">
	<!--spanner-->
	</div>
	<div class="grid_12 mainBox bordered">
		//table here
	</div>
	<div class="clear">
	<!--spanner-->
	</div>
	<div class="grid_12 mainBox centerAlign">
		<input type="submit" value="Save" class="buttons" />	
	</div>
	');
	
	$display = '
	<div class="grid_12 mainBox bordered">
		<div class="grid_4 alpha">
			<h1>Location View</h1>
			<div id="itemListContainer">
				<div id="catListed">
					<select id="locList" name="category" size="6" class="globalSelector" rel="'.INDEX.'items'.EXT.'view=locations">
						<option value="new">--New Location--</option>
						'.$optionVals.'
					</select>
				</div>
			</div>
		</div>
		<div class="grid_8 omega padtop">
			<h3>Help:</h3>
			<p>
				To add/modify location accounts, simply click on the location of your choice or \'New Location\' on the left. The page will reload with options for modification and addition of one or more categories. If you experience any technical difficulties, do not hesitate to send us an inquiry through our website.
			</p>
			<p class="rightAlign">
				- '.logo().'
			</p>
		</div>	
	</div>
	<div class="clear">
	<!--spanner-->
	</div>
	'.$locContents.'
	';
	
	return $display;
}
function displayPage()
{
	$output = (get('view') == 'items' ? itemView() : categoryView());
	$output = (get('view') == 'locations' ? locationView() : $output);
	$output = (get('view') == 'null' ? noAdmin() : $output);
	return adminContent($output, noAdmin());
}
?>